import cv2
from PIL import Image 
import os
import numpy as np
import sys
import numpy as np
from PIL import Image

def show(im_path):
    img = cv2.imread(im_path)
    # img = cv2.resize(img, (int(img.shape[1] / 2), int(img.shape[0] / 2)))
    # cv2.imwrite("_c.".join(im_path.split(".")), img)
    # cv2.imshow("aa", img[:400, :])
    # cv2.imshow("aa", img)
    # cv2.waitKey(0)
    cv2.imwrite("a.jpg", img)

def resize(im_path):
    img = cv2.imread(im_path)
    image = cv2.resize(img, (750, 1334))
    cv2.imwrite("_c.".join(im_path.split(".")), image)

def horizontal_concat(im_path1, im_path2, im_path3):
    img1 = cv2.imread(im_path1)
    img1 = cv2.resize(img1, (860, 1864))
    img2 = cv2.imread(im_path2)
    img2 = cv2.resize(img2, (860, 1864))
    img3 = cv2.imread(im_path3)
    img3 = cv2.resize(img3, (860, 1864))

    total_image = np.concatenate((img1, img2, img3), axis=1)
    cv2.imwrite("3.jpg", total_image)

def vertical_concat(im_path1, im_path2, im_path3):
    img1 = cv2.imread(im_path1)
    img2 = cv2.imread(im_path2)
    img3 = cv2.imread(im_path3)
    total_image = np.concatenate((img1, img2, img3), axis=0)
    cv2.imwrite("out.jpg", total_image)

def show2(im_path):
    image = Image.open(im_path)
    image.show()


def crop_img(data_dir):
    for p in os.listdir(data_dir):
        if p.endswith("png"):
            img_path = data_dir + os.sep + p
            img = cv2.imread(img_path)
            start_x = img.shape[1] // 4
            new_img = img[:, start_x:]

            cv2.imwrite(img_path, new_img)

def rotate_img(data_dir):
    for p in os.listdir(data_dir):
        if p.endswith("jpg"):
            img_path = data_dir + os.sep + p
            img = cv2.imread(img_path)
            new_img = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)   # COUNTER

            cv2.imwrite(img_path, new_img)
        # return 0

if __name__ == "__main__":
    # resize(sys.argv[1])
    # show(sys.argv[1])
    rotate_img(sys.argv[1])
    
    # horizontal_concat(sys.argv[1], sys.argv[2], sys.argv[3])
    # vertical_concat(sys.argv[1], sys.argv[2], sys.argv[3])
    # show(r"C:\Users\swls\Desktop\new1.png")

    # data_dir = "/home/swls/work_dir/ocr/synth/TextRecognitionDataGenerator-master_one/TextRecognitionDataGenerator/background/boeing/4"
    # crop_img(data_dir)
